﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace NewsReader
{
    public partial class RssProviders : PhoneApplicationPage
    {
        List<RssProvider> providers;
        public RssProviders()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            providers = DataProvider.GetRssProviders();
            cmbBoxProvider.ItemsSource = providers;

            cmbBoxProvider.SelectedItem = providers.FirstOrDefault(p => p.Code == App.Settings.ProviderSetting) ?? providers.First(); ;
            cmbBoxProvider.SelectionChanged+=new SelectionChangedEventHandler(cmbBoxProvider_SelectionChanged);
        }
        private void cmbBoxProvider_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (App.Settings.ProviderSetting != ((RssProvider)cmbBoxProvider.SelectedItem).Code)
            {
                App.Settings.ProviderSetting = ((RssProvider)cmbBoxProvider.SelectedItem).Code;
                App.CurrentProvider = providers.FirstOrDefault(p => p.Code == App.Settings.ProviderSetting) ?? providers.First();
                App.IsDataLoaded = false;
                App.Settings.Save();
            }
            NavigationService.GoBack();
        }
    }
}